<!DOCTYPE HTML>
<html>
<head>
    <!-- support for mobile touch devices -->
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">

    <!-- twitter bootstrap CSS stylesheet - not required by cornerstoneTools -->
    <link href="../bootstrap.min.css" rel="stylesheet">

    <link href="../cornerstone.min.css" rel="stylesheet">

</head>
<body>
<div class="container">
    <div class="page-header">
        <h1>
            Stack Loading Progress Example
        </h1>
        <p>
            This page contains an example of monitoring percentage of images in the stack which have been loaded.
        </p>
        <a href="../index.html">Go back to the Examples page</a>
    </div>

    <div class="row">
        <div class="col-xs-12 col-sm-2">
            <h5>Percentage loaded</h5>
            <span id="loadProgress"></span>
        </div>
        <div class="col-xs-6">
            <div style="width:512px;height:512px;position:relative;display:inline-block;color:white;"
                 oncontextmenu="return false"
                 class='cornerstone-enabled-image'
                 unselectable='on'
                 onselectstart='return false;'
                 onmousedown='return false;'>
                <div id="dicomImage"
                     style="width:512px;height:512px;top:0px;left:0px; position:absolute;">
                </div>
            </div>
        </div>

    </div>

</body>

<!-- jquery - included to make things easier to demo, not needed or used by the cornerstone library but
is used by our example image loader-->
<script src="../jquery.min.js"></script>

<!-- include the cornerstone library -->
<script src="../cornerstone.min.js"></script>
<script src="../cornerstoneMath.min.js"></script>

<!-- include the cornerstone tools library -->
<script src="../../dist/cornerstoneTools.js"></script>

<!-- include special code for these examples which provides images -->
<script src="../exampleImageLoader.js"></script>

<script>
    var element = $('#dicomImage').get(0);

    var imageIds = [
        'example://1',
        'example://2',
        'example://3'
    ];

    var stack = {
        currentImageIdIndex : 0,
        imageIds: imageIds
    };


    // Deep copy the imageIds
    var loadProgress = {"imageIds": stack.imageIds.slice(0),
                    "total": stack.imageIds.length,
                    "remaining": stack.imageIds.length,
                    "percentLoaded": 0,
                    };

    function onImageLoaded (event, args){
        var imageId = args.image.imageId;
        var imageIds = loadProgress["imageIds"];

        // Remove all instances, in case the stack repeats imageIds
        for(var i = imageIds.length - 1; i >= 0; i--) {
            if(imageIds[i] === imageId) {
               imageIds.splice(i, 1);
            }
        }

        // Populate the load progress object
        loadProgress["remaining"] = imageIds.length;
        loadProgress["percentLoaded"] = parseInt(100 - loadProgress["remaining"] / loadProgress["total"] * 100, 10);

        if ((loadProgress["remaining"] / loadProgress["total"]) === 0) {
            console.timeEnd("Loading");
        }

        // Write to a span in the DOM
        var currentValueSpan = document.getElementById("loadProgress");
        currentValueSpan.textContent = loadProgress["percentLoaded"];
    }

    // Image loading events are bound to the cornerstone object, not the element
    $(cornerstone).on("CornerstoneImageLoaded", onImageLoaded);

    // image enable the dicomImage element and the mouse inputs
    console.time("Loading");
    cornerstone.enable(element);
    cornerstoneTools.mouseInput.enable(element);
    cornerstoneTools.mouseWheelInput.enable(element);
    cornerstone.loadImage(imageIds[stack.currentImageIdIndex]).then(function(image) {
        // display this image
        cornerstone.displayImage(element, image);

        // set the stack as tool state
        cornerstoneTools.addStackStateManager(element, ['stack']);
        cornerstoneTools.addToolState(element, 'stack', stack);

        // Enable all tools we want to use with this element
        cornerstoneTools.stackScroll.activate(element, 1);
        cornerstoneTools.stackScrollWheel.activate(element);

        // Uncomment below to enable stack prefetching
        // With the example images the loading will be extremely quick, though
        // cornerstoneTools.stackPrefetch.enable(element, 3);
    });

</script>
</html>
